package com.punai.customer.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.utils.PageUtils;
import com.punai.common.utils.SecurityUtils;
import com.punai.customer.domain.CusMaterialCuItem;
import com.punai.customer.domain.CusMaterialItem;
import com.punai.customer.domain.vo.MaterialVo;
import com.punai.customer.mapper.*;
import com.punai.customer.service.ICusMaterialCuItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 材料费用项Service业务层处理
 *
 * @author zxw_
 * @date 2023-01-30
 */
@Service
public class CusMaterialCuItemServiceImpl extends ServiceImpl<CusMaterialCuItemMapper,CusMaterialCuItem> implements ICusMaterialCuItemService
{
    @Autowired
    private CusMaterialCuItemMapper cusMaterialCuItemMapper;
    @Autowired
    private CusLinkMaterialStaCusMapper cusLinkMaterialStaCusMapper;
    /**
     * 查询材料费用项
     *
     * @param id 材料费用项主键
     * @return 材料费用项
     */
    @Override
    public CusMaterialCuItem selectCusMaterialCuItemById(String id)
    {
        return cusMaterialCuItemMapper.selectCusMaterialCuItemById(id);
    }

    /**
     * 查询材料费用项列表
     *
     * @param cusMaterialCuItem 材料费用项
     * @return 材料费用项
     */
    @Override
    public List<CusMaterialCuItem> selectCusMaterialCuItemList(CusMaterialCuItem cusMaterialCuItem)
    {
        return cusMaterialCuItemMapper.selectCusMaterialCuItemList(cusMaterialCuItem);
    }

    /**
     * 新增材料费用项
     *
     * @param cusMaterialCuItem 材料费用项
     * @return 结果
     */
    @Override
    public int insertCusMaterialCuItem(CusMaterialCuItem cusMaterialCuItem)
    {
            return cusMaterialCuItemMapper.insert(cusMaterialCuItem);
    }

    /**
     * 修改材料费用项
     *
     * @param cusMaterialCuItem 材料费用项
     * @return 结果
     */
    @Override
    public int updateCusMaterialCuItem(CusMaterialCuItem cusMaterialCuItem)
    {
        return cusMaterialCuItemMapper.updateById(cusMaterialCuItem);
    }

    /**
     * 批量删除材料费用项
     *
     * @param ids 需要删除的材料费用项主键
     * @return 结果
     */
    @Override
    public int deleteCusMaterialCuItemByIds(String[] ids)
    {
        return cusMaterialCuItemMapper.deleteCusMaterialCuItemByIds(ids);
    }

    /**
     * 删除材料费用项信息
     *
     * @param id 材料费用项主键
     * @return 结果
     */
    @Override
    public int deleteCusMaterialCuItemById(String id)
    {
        return cusMaterialCuItemMapper.deleteCusMaterialCuItemById(id);
    }

    @Autowired
    private CusMaterialCuChapterMapper cusMaterialCuChapterMapper;


    @Override
    public TableDataInfo selectListData(MaterialVo vo) {
        String chapterId = vo.getChapterId();
        String linkFlag = vo.getLinkFlag();
        String itemName = vo.getItemName();
        Long companyId = SecurityUtils.getCompanyId();

        List<CusMaterialCuItem> cusMaterialCuItems = new ArrayList<>();
        Integer itemCount = -1;
        PageUtils.startPage();
        cusMaterialCuItems = cusMaterialCuItemMapper.selectLinkDataList(linkFlag, chapterId, companyId, itemName);
        if (StrUtil.isNotEmpty(chapterId)) {
            List<String> chapterIds = cusMaterialCuChapterMapper.selectChildIdById(chapterId, companyId);
            chapterIds.add(0,chapterId);
            itemCount = cusMaterialCuItemMapper.selectLinkDataListCount(linkFlag, chapterIds, companyId);
        }
        Long total = new PageInfo(cusMaterialCuItems).getTotal();
        TableDataInfo tableDataInfo = new TableDataInfo(cusMaterialCuItems, total.intValue());
        tableDataInfo.setMsg(itemCount.toString());
        return tableDataInfo;
    }

    @Autowired
    private CusMaterialItemMapper cusMaterialItemMapper;
    @Autowired
    private CusMaterialChapterMapper cusMaterialChapterMapper;

    @Override
    public TableDataInfo selectMaterialList(MaterialVo vo) {
        String linkFlag = vo.getLinkFlag();
        String chapterId = vo.getChapterId();
        String itemName = vo.getItemName();
        Long companyId = SecurityUtils.getCompanyId();
        List<CusMaterialItem> cusMaterialItems = new ArrayList<>();
        // 需要
        Long total = 0L;
        Integer itemCount = -1;

        PageUtils.startPage();
        cusMaterialItems = cusMaterialItemMapper.selectLinkFlagData(linkFlag,chapterId,companyId,itemName);
        if (StrUtil.isNotEmpty(chapterId)) {
            List<String> chapterIds = cusMaterialChapterMapper.selectChildrenIdById(chapterId, companyId);
            chapterIds.add(chapterId);
            itemCount = cusMaterialItemMapper.selectLinkFlagCount(linkFlag, chapterIds, companyId);
        }
        total = new PageInfo(cusMaterialItems).getTotal();
        TableDataInfo tableDataInfo = new TableDataInfo(cusMaterialItems, total.intValue());
        tableDataInfo.setMsg(itemCount.toString());
        return tableDataInfo;
    }



}
